package com.example.f8399.testdemo.activity;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.example.f8399.testdemo.BaseActivity;
import com.example.f8399.testdemo.R;
import com.example.f8399.testdemo.databinding.ActivityViewPager1Binding;
import com.example.f8399.testdemo.fragment.FirstFragment;
import com.example.f8399.testdemo.fragment.SecondFragment;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class ViewPager1Activity extends BaseActivity {

    ActivityViewPager1Binding mBinding;
    private FragmentManager mManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_view_pager1);

        initView();
    }

    private void initView() {
        List<Fragment> list = new ArrayList<>();
        list.add(FirstFragment.newIntants());
        list.add(SecondFragment.newInstance());
        mManager = this.getSupportFragmentManager();
        mBinding.viewPager.setAdapter(new ViewPager1Adaper(mManager, list));
        mBinding.tabLayout.setupWithViewPager(mBinding.viewPager);
        setTabLine(mBinding.tabLayout, 50, 50);
        addTabDivider(mBinding.tabLayout,10);
    }

    private void addTabDivider(TabLayout tabLayout, int divider_width) {
        //我也很疑惑为什么不用for循环而是直接指定index 0 就可以了
        //参考1：https://blog.csdn.net/qq_26413249/article/details/54093288
        //参考2：https://blog.csdn.net/wlxfxy/article/details/73732574
        LinearLayout childAt = (LinearLayout) tabLayout.getChildAt(0);
            childAt.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
            childAt.setDividerDrawable(ContextCompat.getDrawable(this,R.drawable.divider_1dp));
    }

    private class ViewPager1Adaper extends FragmentPagerAdapter {
        private String[] titles = {"lcfeng1", "lcfeng2"};


        private List<Fragment> mFragmentList = new ArrayList<>();

        public ViewPager1Adaper(FragmentManager fm, List<Fragment> plist) {
            super(fm);
            mFragmentList = plist;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }

    /*public static void setIndicator(Context context, TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout ll_tab = null;
        try {
            ll_tab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = SizeUtils.dp2px(leftDip);
        int right = SizeUtils.dp2px(rightDip);

        for (int i = 0; i < ll_tab.getChildCount(); i++) {
            View child = ll_tab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }*/
    public void setTabLine(TabLayout tab, int left, int right) {
        try {
            Class<?> tablayout = tab.getClass();
            Field tabStrip = tablayout.getDeclaredField("mTabStrip");
            tabStrip.setAccessible(true);
            LinearLayout ll_tab = (LinearLayout) tabStrip.get(tab);
            for (int i = 0; i < ll_tab.getChildCount(); i++) {
                View child = ll_tab.getChildAt(i);
                child.setPadding(0, 0, 0, 0);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
                //修改两个tab的间距
                params.setMarginStart(SizeUtils.dp2px(left));
                params.setMarginEnd(SizeUtils.dp2px(right));
                child.setLayoutParams(params);
                child.invalidate();
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
            LogUtils.e(e.toString());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            LogUtils.e(e.toString());

        }
    }
}
